import pyssdb
import json
import time
import os
import psutil
import logging
import traceback
from logging.handlers import RotatingFileHandler
from squirrel_bakend.task.task_logic import start_scrapy_spider


def configure_file_logging():
    log_dir = os.environ.get("LOG_PATH", "/")
    full_path = os.path.join(log_dir, "squirrel_bakend")
    if not os.path.isdir(full_path):
        os.makedirs(full_path)
    log_file = os.path.join(full_path, "cycle_task.log")
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    for handler in logger.handlers[:]:
        logger.removeHandler(handler)
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)
    file_handler = RotatingFileHandler(
        filename=log_file,
        maxBytes=10 * 1024 * 1024,
        backupCount=5,
        encoding="utf-8"
    )

    formatter = logging.Formatter(
        "%(asctime)s | %(levelname)-8s | %(name)s:%(lineno)d | %(message)s"
    )
    file_handler.setFormatter(formatter)
    logger.addHandler(console_handler)
    logger.addHandler(file_handler)

    return logger


def run():
    logger = configure_file_logging()
    client = pyssdb.Client(host="1.14.96.163", port=8888)
    try:
        while True:
            cpu_percent = psutil.cpu_percent(interval=1)
            mem_percent = psutil.virtual_memory().percent
            if cpu_percent <= 80 or mem_percent <= 70:
                logger.info("准备从队列中读取任务")
                if client.qsize("task_queue_vip"):
                    queue_name = "task_queue_vip"
                else:
                    queue_name = "task_itra_queue"
                params = client.qpop_front(queue_name)
                if params:
                    logger.info(f"从{queue_name}中读取的任务为：{json.loads(params)}")
                    params = json.loads(params)
                    data = start_scrapy_spider(params)
                    logger.info(f"任务【{params}】请求结果: {data}")
                    time.sleep(0.5)
                else:
                    logger.info(f"任务队列【{queue_name}】为空,等待2s")
                    time.sleep(2)
            else:
                logger.info(f"资源紧张[cpu:{cpu_percent},mem:{mem_percent}]，等待10s")
                time.sleep(10)
    except Exception:
        logger.info(f"处理队列任务时出错：{traceback.format_exc()}")
    finally:
        time.sleep(10)
        if client:
            client.disconnect()


if __name__ == '__main__':
    run()
